package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.bean.OrderNowBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.dlwx.firefighting.utils.glide.GlideImageLoader;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.NormalListDialog;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 订单详情页面
 */
public class OrderDetailsNowActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.order_details_fukuan)
    TextView btnFukuan;
    @BindView(R.id.order_details_cancel)
    TextView btnCancel;
    @BindView(R.id.name_tv)
    TextView nameTv;
    @BindView(R.id.phone_tv)
    TextView phoneTv;
    @BindView(R.id.address_tv)
    TextView addressTv;
    @BindView(R.id.recycler)
    LRecyclerView recyclerView;
    @BindView(R.id.order_num_tv)
    TextView orderNumTv;

    @BindView(R.id.goods_money_tv)
    TextView goodsMoneyTv;
    @BindView(R.id.express_money_tv)
    TextView expressMoneyTv;
    @BindView(R.id.money_tv)
    TextView moneyTv;


    private GlideImageLoader imageLoader;
//    private OrderDetailAdapter mAdapter;

    private int loadType;
    String orderId="";
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_now);
        ButterKnife.bind(this);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        orderId=getIntent().getStringExtra("id");
        OrderNowBean bean= (OrderNowBean) getIntent().getSerializableExtra("bean");
        initView();
        setDatas(bean);
    }

    private void initView() {
        imageLoader = new GlideImageLoader(this);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("支付");
       /* mAdapter = new OrderDetailAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.setDataList(new ArrayList<OrderDetailBean.XiangBean>());
        LRecyclerViewAdapter lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        lRecyclerViewAdapter.removeFooterView();
        lRecyclerViewAdapter.removeHeaderView();
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setPullRefreshEnabled(false);
        DividerDecoration divider = new DividerDecoration.Builder(this)
                .setHeight(R.dimen.divider_height)
                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.line_gray)
                .build();
        recyclerView.addItemDecoration(divider);
        recyclerView.refresh();*/

    }


    private void setDatas(OrderNowBean bean) {
        OrderNowBean.AddressBean address = bean.getAddress();
        addressId=bean.getAddress().getAddress_id();
        nameTv.setText("收货人：" + bean.getAddress().getConsignee());
        addressTv.setText("收货地址：" +address.getAddress());
        phoneTv.setText( address.getMobile());

        goodsMoneyTv.setText(getString(R.string.rmb) + bean.getGoods_total());
        expressMoneyTv.setText(getString(R.string.rmb) + bean.getShipping_free());
        moneyTv.setText(getString(R.string.rmb) + bean.getTotal());
        btnFukuan.setText("付款");
        goodsNum=bean.getNum();
        shippingFee=bean.getShipping_free();
        float f = 0;
        try {
             f=Float.parseFloat(bean.getTotal()+"");
        }catch (Exception e){
            e.printStackTrace();
        }
        total=f;
       /* if(!lists.isEmpty()){
            goodsId=lists.get(0).getGoods_id();

        }*/
    /*    mAdapter.addAll(bean.getGoods());

        recyclerView.refresh();*/
    }


    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @OnClick({R.id.title_left_one_btn, R.id.order_details_fukuan, R.id.order_details_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                finish();
                break;
            case R.id.order_details_fukuan:
                    doPay();
                break;
            case R.id.order_details_cancel:
                cancelOrder();
                break;
        }
    }



    private String goodsId;
    private String addressId;
    private float total;
    private String shippingName;
    private String shippingFee;
    private String payName;
    private String goodsNum;

    private void doPay() {
//        支付方式4，余额支付
        OkGo.<String>post(Constant.Pay_ORDER)
                .tag(this)
                .params("user_id", userId)
                .params("orderid", orderId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        BaseBean bean = dealResult.dealBaseRes(OrderDetailsNowActivity.this, response);
//                        if(bean==null){
//                            return;
//                        }
//                        if(bean.getStatus()==Constant.SUCESS){
//                           ToastUtils.toastForShort(OrderDetailsNowActivity.this,"支付成功");
//                        }else{
//
//                            ToastUtils.toastForShort(OrderDetailsNowActivity.this,bean.getMsg());
//                        }
//                        OrderDetailsNowActivity.this.finish();
//                        startActivity(new Intent(OrderDetailsNowActivity.this,UserOrderActivity.class));

                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(OrderDetailsNowActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });

    }
    String[] mStringItems = {"我不想买了", "信息填写错误，重新拍", "卖家缺货", "同城见面交易", "其他原因"};

    private void cancelOrder() {
        final NormalListDialog dialog = DialogUtils.getNormalListDialogStringArr(OrderDetailsNowActivity.this, "取消订单", mStringItems);
        dialog.setOnOperItemClickL(new OnOperItemClickL() {
            @Override
            public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog.dismiss();
                ToastUtils.toastForLong(OrderDetailsNowActivity.this, mStringItems[position]);
            }
        });

    }

    //--------------------------------------------------------------
}
